package com.lcredu.springbootusersys.controller;

import com.lcredu.springbootusersys.bean.Admin;
import com.lcredu.springbootusersys.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.ArrayList;

/**
 * @author 黎长容
 * @date 2025-08-14 18:31
 */
@Controller
@Slf4j
public class AdminController {


    @PostMapping("/login")
    public String login(Admin admin, Model model, HttpSession session) {
        System.out.println("ok");
        //查找映射路径为/manage.html的方法
        //如果账号不为空,密码为666即可通过
        if (StringUtils.hasText(admin.getName()) && "666".equals(admin.getPassword())) {
            //登陆成功,返回到管理页面
            log.info("登录用户为{}", admin.getName());
            //记录session
            session.setAttribute("admin", admin);
            //请求重定向
            return "redirect:/manage.html";
        } else {
            //登陆失败,返回到登录页面
            //携带错误信息
            model.addAttribute("msg", "登录错误");
            return "login";
        }

    }

    @GetMapping("/manage.html")
    public String manage(Model model, HttpSession session) {
        //获取数据
//        if (session.getAttribute("admin") != null) {
            ArrayList<User> users = new ArrayList<>();
            users.add(new User(1, "关羽~", "666666", 20, "gy@sohu.com"));
            users.add(new User(2, "张飞", "666666", 30, "zf@sohu.com"));
            users.add(new User(3, "赵云", "666666", 22, "zy@sohu.com"));
            users.add(new User(4, "马超", "666666", 28, "mc@sohu.com"));
            users.add(new User(5, "黄忠", "666666", 50, "hz@sohu.com"));
            model.addAttribute("users", users);
            return "manage";
        } 
//        else {
//            model.addAttribute("msg", "请先登录");
//            return "login";
//        }
//    }
}
